//
//  FBDPhotoManger.m
//  iOS9Sample-Photos
//
//  Created by feng on 2018/4/7.
//  Copyright © 2018年 小码哥. All rights reserved.
//

#import "FBDPhotoManger.h"

@implementation FBDPhotoManger
/**
 单列类
 
 @return 实例化的单例对象
 */
+(instancetype)shareManager
{
    static FBDPhotoManger *manager=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager=[[self alloc]init];
        manager.selectAssetIDPhotoArray=[NSMutableArray array];
    });
    return manager;
}


/**
 查询用户的所有相册
 */
-(void)searchUserMobileAllCollectionComplained:(void(^)(NSMutableArray<PHAssetCollection*>*assetCollectionArray))complainedBlock
{
    NSMutableArray* listAlbumArray=[NSMutableArray array];
    
    dispatch_queue_t queue =dispatch_queue_create("come.fengbaodong.www", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        PHFetchResult<PHAssetCollection *> *collectionResult0 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
        for (PHAssetCollection *collection in collectionResult0)
        {
            [listAlbumArray addObject:collection];
        }
        PHFetchResult<PHAssetCollection *> *collectionResult1 = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
        for (PHAssetCollection *collection in collectionResult1)
        {
            [listAlbumArray addObject:collection];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complainedBlock) {
                complainedBlock(listAlbumArray);
            }
        });
    });
    
}

/*
 *
 * 查询某个相册里面的封面 （最近拍摄的那张图）
 */
- (void)searchLastImagesInCollection:(PHAssetCollection *)assetCollection  withBlcok:(void (^)(UIImage *__nullable result, NSInteger totalPic  ))resultHandler
{
    NSLog(@"相簿名:%@", assetCollection.localizedTitle);
    
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
    // 同步获得图片, 只会返回1张图片
    options.synchronous = YES;
    
    // 获得某个相簿中的所有PHAsset对象
    PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:nil];
    //    for (PHAsset *asset in assets) {
    // 是否要原图
    PHAsset* asset=[assets lastObject];
    CGSize size = CGSizeMake(asset.pixelWidth/10, asset.pixelHeight/10);
    
    // 从asset中获得图片
    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:size contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        NSLog(@"%@", result);
         BOOL isiCloudAsset=NO;
        //根据请求会调中的参数重 NSDictionary *info 是否有cloudKey 来判断是否是  iCloud
        if ([[info objectForKey: PHImageResultIsInCloudKey] boolValue])
        {
            isiCloudAsset = YES;
        }
        NSLog(@"isiCloudAsset:>>>>>>%d",isiCloudAsset);
        if (resultHandler) {
            resultHandler(result,assets.count);
        }
    }];
    
    
}


/*
 *
 * 查询某个相册里面的所有图片
 */
- (void)searchAllImagesInCollection:(PHAssetCollection *)collection
{
    
    
    
}












@end
